package com.dk.controller;

import com.alibaba.fastjson.JSON;
import com.dk.pojo.CarType;
import com.dk.service.CarTpeyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("carType")
public class CarTypeController {
    @Autowired
    private CarTpeyService carTpeyService;

    private Logger logger = LoggerFactory.getLogger(CarTypeController.class);

    @RequestMapping("toCarTypeList")
    public String toCarTypeList(){
        return "carType/list";
    }

    @RequestMapping("getCarTypeAllInfo")
    @ResponseBody
    public List<CarType> getCarTypeAllInfo(){
        try{
            logger.info("进入查询所有汽车类型方法");
            List<CarType> list = carTpeyService.getCarTypeInfo();
            return list;
        }catch (Exception e){
            logger.info("查询所有汽车数据异常",e);
            return null;
        }
    }

    @RequestMapping("toAddPage")
    public String toAddPage(){
        return "carType/add";
    }

    @RequestMapping("addCarType")
    @ResponseBody
    public void addCarType(CarType ct){
        try{
            logger.info("进入增加汽车类型方法，参数：{}", JSON.toJSONString(ct));
            carTpeyService.addCarType(ct);
        }catch (Exception e){
            logger.info("增加汽车数据异常",e);
        }
    }

    @RequestMapping("toUpdatePage")
    public String toUpdatePage(Integer tid, Model model){
        try{
            logger.info("进入跳转汽车类型修改页面，根据汽车类型ID查询汽车数据，参数：{}", tid);
            CarType ct = carTpeyService.getCarTypeById(tid);
            model.addAttribute("ct",ct);
            return "carType/update";
        }catch (Exception e){
            logger.info("查询汽车类型异常",e);
            return "carType/update";
        }
    }

    @RequestMapping("updateCarType")
    @ResponseBody
    public void updateCarType(CarType ct){
        try{
            logger.info("进入修改汽车类型方法，参数：{}", JSON.toJSONString(ct));
            carTpeyService.updateCarType(ct);
        }catch (Exception e){
            logger.info("修改汽车类型异常",e);
        }
    }

    @RequestMapping("deleteCarType")
    @ResponseBody
    public void deleteCarType(Integer tid){
        try {
            logger.info("进入根据汽车类型ID删除汽车类型数据方法，参数{}",tid);
            carTpeyService.deleteCarType(tid);
        }catch (Exception e){
            logger.error("删除汽车类型数据异常",e);
        }
    }

}
